#!/bin/sh
# fengyuanzhao @sztozed
# mutil apn dial shell for opwnwrt 
# version 1.0.1
# 2019.03.20
local REVAT_FILE="/tmp/mutilapn_revat"
local SENDAT_FILE="/tmp/mutilapn_sendat"
local AT_SERIAL_PORT="/dev/ttyUSB1"
local MUTILAPN_OUTPUT_INFO_FILE="/tmp/.mutilapn_info"

send_at()
{
	echo $1 > ${SENDAT_FILE}
	> ${REVAT_FILE}	
	sendat -d${AT_SERIAL_PORT} -f${SENDAT_FILE} -o${REVAT_FILE}  > /dev/null 

	local ret=`cat ${REVAT_FILE} | grep OK | wc -l`
	if [ $ret'' == 1 ];then
		return 0
	fi	
	return 1

}

check_udhcpc()
{
	local pid=`pgrep -f "udhcpc.*$1" `
	logger "check_udhcpc:$1 kill $pid"
	for temp in $pid
	do
		kill -9 $temp
	done	

	sleep 1
	local count=`ps | grep udhcpc | grep $1 | grep -v grep | wc -l`	
	if [ $count -eq 0 ];then
		logger "up udhcpc for $1"
		udhcpc -b -i $1   -s /etc/udhcpc.script
	fi
}

check_odhcp6c()
{
	local pid=`pgrep -f "odhcp6c.*$1" `
	logger "check_odhcp6c:$1 kill $pid"
	for temp in $pid
	do
		kill -9 $temp
	done	

	sleep 1
	local count=`pgrep -f "odhcp6c.*$1" | wc -l`	
	if [ $count -eq 0 ];then
		logger "up odhcp6c for $1"
		odhcp6c -s /lib/netifd/dhcpv6.script -P0 -t120 $1 &
	fi
}

check_ifname()
{
	local count=`ifconfig -a | grep $1 | wc -l`
	if [ $count -eq 0 ];then
		return 1	
	fi
	return 0
}




dial_mutilapn1()
{
	if [ $TZ_ENABLE_AUTO_DIAL'' != '0' -a $TZ_MUTILAPN1_ENABLE'' == '1'   -a $TZ_MUTILAPN1_IP_STACK_MODE"" != ""  -a $TZ_MUTILAPN1_APN_NAME"" != "" ];then

		send_at "at+cgdcont=2,\"$TZ_MUTILAPN1_IP_STACK_MODE\",\"$TZ_MUTILAPN1_APN_NAME\""
		logger "dial_mutilapn1:send_at at+cgdcont=2,\"$TZ_MUTILAPN1_IP_STACK_MODE\",\"$TZ_MUTILAPN1_APN_NAME\"  ret = $? "
		#send_at "at\$qcpdpp=2,$TZ_MUTILAPN1_AUTH_TYPE,\"$TZ_MUTILAPN1_USERNAME\",\"$TZ_MUTILAPN1_PASSWORD\""	
		#logger "dial_mutilapn1:send_at at\$qcpdpp=2,$TZ_MUTILAPN1_AUTH_TYPE,\"$TZ_MUTILAPN1_USERNAME\",\"$TZ_MUTILAPN1_PASSWORD\" ret = $?"
		
		local ifname=`uci get network.4g1.ifname` 
		check_ifname $ifname
		if [ $? -eq 0 ];then

		
			if [ $TZ_MUTILAPN1_IP_STACK_MODE'' == "IP" -o  $TZ_MUTILAPN1_IP_STACK_MODE'' == "IPV4" ];then
				logger "dial_mutilapn1:dial IPV4 for $ifname"

				send_at "at\$qcrmcall=0,2"                                                                                                     

				check_udhcpc $ifname
				send_at "at\$qcrmcall=1,2,1,2,2"
				logger "dial_mutilapn1:send_at at\$qcrmcall=1,2,1,2,2 ret = $?"
			elif [ $TZ_MUTILAPN1_IP_STACK_MODE'' == "IPV6" ];then
				logger "dial_mutilapn1:dial IPV6 for $ifname"

				ip addr add fe80:c810:3001:d00::4/56 dev $ifname

				send_at "at\$qcrmcall=0,2,2" 

				send_at "at\$qcrmcall=1,2,2,2,2" &

				check_odhcp6c $ifname 

				logger "dial_mutilapn1:send_at at\$qcrmcall=1,2,2,2,2 ret = $?"
			else
				logger "dial_mutilapn1:dial IPV4V6 for $ifname"

				ip addr add fe80:c810:3001:d00::4/56 dev $ifname

				send_at "at\$qcrmcall=0,2,3"                                                                                                     

				check_udhcpc $ifname

				send_at "at\$qcrmcall=1,2,3,2,2" &

				check_odhcp6c $ifname 

				logger "dial_mutilapn1:send_at at\$qcrmcall=1,2,3,2,2 ret = $?"
			fi

			if [ $TZ_MUTILAPN1_MTU"" != "" ];then
				ifconfig $ifname  mtu $TZ_MUTILAPN1_MTU
			fi
		else
			logger "dial_mutilapn1:ERROR $ifname not found"
		fi

	fi 
}

dial_mutilapn2()
{
	if [ $TZ_ENABLE_AUTO_DIAL'' != '0' -a  $TZ_MUTILAPN2_ENABLE'' == '1'  -a $TZ_MUTILAPN2_IP_STACK_MODE"" != ""  -a $TZ_MUTILAPN2_APN_NAME"" != "" ];then

		send_at "at+cgdcont=3,\"$TZ_MUTILAPN2_IP_STACK_MODE\",\"$TZ_MUTILAPN2_APN_NAME\""
        logger "dial_mutilapn2:send_at at+cgdcont=3,\"$TZ_MUTILAPN2_IP_STACK_MODE\",\"$TZ_MUTILAPN2_APN_NAME\"  ret = $? "                                                                          
        #send_at "at\$qcpdpp=3,$TZ_MUTILAPN2_AUTH_TYPE,\"$TZ_MUTILAPN2_USERNAME\",\"$TZ_MUTILAPN2_PASSWORD\""
        #logger "dial_mutilapn2:send_at at\$qcpdpp=3,$TZ_MUTILAPN2_AUTH_TYPE,\"$TZ_MUTILAPN2_USERNAME\",\"$TZ_MUTILAPN2_PASSWORD\" ret = $? "                                                                                       
                                                                                                     
        local ifname=`uci get network.4g2.ifname`                                                    
        check_ifname $ifname                                                                         
        if [ $? -eq 0 ];then                                                                         
                                                                                                     
                                                             
                                                                                                 
            if [ $TZ_MUTILAPN2_IP_STACK_MODE'' == "IP" -o  $TZ_MUTILAPN2_IP_STACK_MODE'' == "IPV4" ];then
                    logger "dial_mutilapn2:dial IPV4 for $ifname"      
                    
                    send_at "at\$qcrmcall=0,3"                                                                                                     

            		check_udhcpc $ifname     

                    send_at "at\$qcrmcall=1,3,1,2,3"                                             
                    logger "dial_mutilapn2:send_at at\$qcrmcall=1,3,1,2,3 ret = $?"                                                                      
            elif [ $TZ_MUTILAPN2_IP_STACK_MODE'' == "IPV6" ];then
            		logger "dial_mutilapn2:dial IPV6 for $ifname"     

            		ip addr add fe80:c810:3001:d00::5/56 dev $ifname

					send_at "at\$qcrmcall=0,3,2" 

                    send_at "at\$qcrmcall=1,3,2,2,3"   &                              

                    check_odhcp6c $ifname

                    logger "dial_mutilapn2:send_at at\$qcrmcall=1,3,2,2,3 ret = $?"                                                                       
            else                                                                                 
                    logger "dial_mutilapn2:dial IPV4V6 for $ifname" 

                    ip addr add fe80:c810:3001:d00::5/56 dev $ifname

                    send_at "at\$qcrmcall=0,3,3" 

                    check_udhcpc $ifname
					
                    send_at "at\$qcrmcall=1,3,3,2,3" &

                   	check_odhcp6c $ifname

                    logger "dial_mutilapn2:send_at at\$qcrmcall=1,3,3,2,3 ret = $?"                                                                      
            fi   

            if [ $TZ_MUTILAPN2_MTU"" != "" ];then
				ifconfig $ifname  mtu $TZ_MUTILAPN2_MTU
			fi                                                                                
        else                                                                                         
            logger "dial_mutilapn2:ERROR $ifname not found"                                                             
        fi                                                                                           


	fi
}

. /lib/functions.sh
local wan_interfaces=""
local wan_sections=""
local wan_sections_v6=""
local wan_interfaces_v6=""
load_interface() {
        local cfgfile="$1"
        DEVICES=
        config_cb() {
                local type="$1"
                local section="$2"

                config_get TYPE "$CONFIG_SECTION" TYPE
                case "$TYPE" in
                        interface)
						local type=""
						local ifname=""
		                config_get ifname "$CONFIG_SECTION" ifname
		                config_get proto "$CONFIG_SECTION" proto
		                config_get type "$CONFIG_SECTION" type

						if [ $type"" != "bridge"  -a $CONFIG_SECTION != "loopback" -a `echo $wan_interfaces | grep $ifname |wc -l ` == "0" ]
						then
							if [ $proto"" == "dhcp" ];then
	                        	wan_sections="$wan_sections $CONFIG_SECTION"
								wan_interfaces="$wan_interfaces $ifname"
							elif [ $proto"" == "dhcpv6" ];then
	                        	wan_sections_v6="$wan_sections_v6 $CONFIG_SECTION"
								wan_interfaces_v6="$wan_interfaces_v6 $ifname"
							fi
						fi
						
                    ;;
                esac                     
        }                                
        config_load "${cfgfile:-network}"
} 

local ssid=""
get_ssid_by_lan_section()
{
	for section in `uci show wireless  | grep wifi-iface | grep network | cut -d'.' -f 2`
	do
		local temp_network=`uci get wireless.$section.network`
		[ $temp_network'' == $1'' ] && { 
			ssid="$ssid `uci get wireless.$section.ssid`" 
		}
	done

	for section in `uci show mutilssid  | grep wifi-iface | grep network | cut -d'.' -f 2`
	do
		local temp_network=`uci get mutilssid.$section.network`
		[ $temp_network'' == $1'' ] && { 
			ssid="$ssid `uci get mutilssid.$section.ssid`" 
		}
	done

}

refresh_info()
{
	send_at "at\$qcrmcall?"
	
	local ret=`cat ${REVAT_FILE} | grep "$1" | wc -l`	

	if [ ! -f $MUTILAPN_OUTPUT_INFO_FILE ];then
		touch $MUTILAPN_OUTPUT_INFO_FILE
	fi
	sed -i /^APN$1/d $MUTILAPN_OUTPUT_INFO_FILE

	if [ $ret"" != '0' ];then
		local ip=`sendat -d${AT_SERIAL_PORT} -e at+cgpaddr=$1 | grep '+CGPADDR:' | cut -d',' -f 2`
		ip=`echo $ip | sed s/[[:space:]]//g`
		if [ $ip'' != '' ];then
			local netmask=`sendat -d${AT_SERIAL_PORT} -e at+dhcp4? | grep "$ip" | cut -d':' -f 2 | cut -d',' -f 2`
			local gateip=`sendat -d${AT_SERIAL_PORT} -e at+dhcp4? | grep "$ip" | cut -d':' -f 2 | cut -d',' -f 3`
			local dns1=`sendat -d${AT_SERIAL_PORT} -e at+dhcp4? | grep "$ip" | cut -d':' -f 2 | cut -d',' -f 4`
			local dns2=`sendat -d${AT_SERIAL_PORT} -e at+dhcp4? | grep "$ip" | cut -d':' -f 2 | cut -d',' -f 5`
			local ifname=""
			local dhcp_gate_ip
			local dhcp_gate_netmask

			for temp in $wan_sections
			do
				local temp_ifname=""
				temp_ifname=`uci get network.$temp.ifname`
				local temp_ip=`ifconfig $temp_ifname | grep "inet addr" | awk '{print $2}' | cut -d':' -f 2`
				temp_ip=`echo $temp_ip | sed s/[[:space:]]//g`
				if [ $temp_ip'' != $ip ];then

					temp_ip=`ifconfig $temp_ifname | grep "inet6 addr" | awk '{print $3}'`
					temp_ip=`echo $temp_ip | sed s/[[:space:]]//g`
					[ $temp_ip'' != $ip'' ] && {
						continue;
					}
					[ $temp_ip'' == $ip'' ] && {
						ifname=$temp_ifname
					}
				else
					ifname=$temp_ifname ;
				fi

				local lan_section=""
				lan_section=`uci get network.$temp.route_lan_section`
				dhcp_gate_ip=`uci get network.$lan_section.ipaddr`
				dhcp_gate_netmask=`uci get network.$lan_section.netmask`
				ssid=""
				get_ssid_by_lan_section $lan_section

				local temp_network=`/bin/ipcalc.sh $dhcp_gate_ip $dhcp_gate_netmask | grep "NETWORK" | cut -d'=' -f 2`
				local temp_netmask=`/bin/ipcalc.sh $dhcp_gate_ip $dhcp_gate_netmask | grep "PREFIX" | cut -d'=' -f 2`
				local route_table=`ip rule | grep $temp_network/$temp_netmask | grep "all to" | awk '{print $7}'`

				if [ $route_table"" == $ifname ];then
					echo "APN$1:$temp,$ip,$netmask,$ifname,$temp_network,$dhcp_gate_netmask,$ssid" >> $MUTILAPN_OUTPUT_INFO_FILE
				else
					echo "APN$1:$temp,$ip,$netmask,$ifname,,," >> $MUTILAPN_OUTPUT_INFO_FILE
				fi
				
			done
		fi
	
	else
		local wan_sec_name="4g"
		if [ $1'' == "1" ];then
			wan_sec_name="4g"
		elif [ $1"" == "2" ];then
			wan_sec_name="4g1"
		else
			wan_sec_name="4g2"
		fi
		
		local ifname=`uci get network.$wan_sec_name.ifname`
		local lan_section=`uci get network.$wan_sec_name.route_lan_section`
		dhcp_gate_ip=`uci get network.$lan_section.ipaddr`
		dhcp_gate_netmask=`uci get network.$lan_section.netmask`
		ssid=""
		get_ssid_by_lan_section $lan_section
		local network=`/bin/ipcalc.sh $dhcp_gate_ip $dhcp_gate_netmask | grep "NETWORK" | cut -d'=' -f 2`
		local netmask=`/bin/ipcalc.sh $dhcp_gate_ip $dhcp_gate_netmask | grep "PREFIX" | cut -d'=' -f 2`
		local route_table=`ip rule | grep $network/$netmask | grep "all to" | awk '{print $7}'`

		if [ $route_table"" == $ifname ];then
			echo "APN$1:$wan_sec_name,,,$ifname,$network,$dhcp_gate_netmask,$ssid" >> $MUTILAPN_OUTPUT_INFO_FILE
		else
			echo "APN$1:$wan_sec_name,,,$ifname,,," >> $MUTILAPN_OUTPUT_INFO_FILE
		fi
	fi
}

refresh_eth()
{
	for temp in $wan_sections
	do

		local ret=`uci get network.$temp.land_wan 2>/dev/null`

		if [ $ret"" != "1" ];then
			continue
		fi

		local ifname=`uci get network.$temp.ifname`
		

		local lan_section=`uci get network.$temp.route_lan_section`
		local dhcp_gate_ip=`uci get network.$lan_section.ipaddr`
		local dhcp_gate_netmask=`uci get network.$lan_section.netmask`
		local ssid=""
		get_ssid_by_lan_section $lan_section

		
		local network=`/bin/ipcalc.sh $dhcp_gate_ip $dhcp_gate_netmask | grep "NETWORK" | cut -d'=' -f 2`
		local netmask=`/bin/ipcalc.sh $dhcp_gate_ip $dhcp_gate_netmask | grep "PREFIX" | cut -d'=' -f 2`
		local route_table=`ip rule | grep $network/$netmask | grep "all to" | awk '{print $7}'`

		local temp_ip=`ifconfig $ifname | grep "inet addr" | awk '{print $2}' | cut -d':' -f 2`
		temp_ip=`echo $temp_ip | sed s/[[:space:]]//g`
		local temp_netmask=`ifconfig $ifname | grep "inet addr" | awk '{print $4}' | cut -d':' -f 2`
		temp_netmask=`echo $temp_netmask | sed s/[[:space:]]//g`

		if [ $route_table"" == $ifname ];then
			#sed -i /^APN1/d $MUTILAPN_OUTPUT_INFO_FILE

			sed -i /^ETH/d $MUTILAPN_OUTPUT_INFO_FILE
			echo "ETH:$temp,$temp_ip,$temp_netmask,$ifname,$network,$dhcp_gate_netmask,$ssid" >> $MUTILAPN_OUTPUT_INFO_FILE
		else
			sed -i /^ETH/d $MUTILAPN_OUTPUT_INFO_FILE
			echo "ETH:$temp,$temp_ip,$temp_netmask,$ifname,,," >> $MUTILAPN_OUTPUT_INFO_FILE
		fi
	done


}

check_4g_network()
{
	while [ 1 ]
	do
		send_at "at+bmrat"	
		local ret=`cat ${REVAT_FILE} | grep NONE | wc -l`
		if [ $ret -eq '0' ];then
			break;
		else
			logger "check_4g_network:NOT REGISTED!"
		fi
			
		sleep 5
	done


}

add_able_default_route()
{
	ping -w 3 8.8.8.8 -I $1 > /dev/null

	if [ $? == "0" ];then

		local ret=`route | grep default | awk '{print $8}'`
		if [ $ret"" != "" ];then
		
			for target in `ip route | grep $ret | awk '{print $1}'`
			do
				ip route del $target
			done
		fi 


		local count=`ip route list table $1 | wc -l`

		while [ $count -gt 0 ]
		do
			local arg=`ip route list table $1 | sed -n ${count}p`
			ip route add $arg
			count=$(($count - 1))
		done 

		return 0
	else
		return 1
	fi

}

check_route()
{
	ping -w 3 8.8.8.8 > /dev/null
	if [ $? != '0' ];then

		local land_wan_if
		local primary_apn_if
		local primary_lan_network
		local primary_lan_netmask
		local route_lan_section
		local ipaddr

		for temp in $wan_sections;do
			config_get land_wan $temp land_wan
			if [ $land_wan"" == "1" ];then
				config_get land_wan_if $temp ifname

				config_get route_lan_section $temp route_lan_section
				config_get ipaddr $route_lan_section ipaddr
				config_get netmask $route_lan_section netmask
				primary_lan_network=`/bin/ipcalc.sh $ipaddr $netmask | grep "NETWORK" | cut -d'=' -f 2`
				primary_lan_netmask=`/bin/ipcalc.sh $ipaddr $netmask | grep "PREFIX" | cut -d'=' -f 2`
				
			fi

			config_get primary_apn $temp primary_apn

			if [ $primary_apn"" == "1" ];then
				config_get primary_apn_if $temp ifname
			fi
		done

		if [ $land_wan_if"" == "" ];then
			config_get land_wan_if 'wan' ifname
			config_get route_lan_section 'wan' route_lan_section
			config_get ipaddr $route_lan_section ipaddr
			config_get netmask $route_lan_section netmask
			primary_lan_network=`/bin/ipcalc.sh $ipaddr $netmask | grep "NETWORK" | cut -d'=' -f 2`
			primary_lan_netmask=`/bin/ipcalc.sh $ipaddr $netmask | grep "PREFIX" | cut -d'=' -f 2`
		fi

		if [ $primary_apn_if"" == "" ];then
			config_get primary_apn_if '4g' ifname
		fi

		ret=""
		add_able_default_route $land_wan_if
		ret=$?

		if [ $ret"" == "0" ];then
			ip rule del to $primary_lan_network/$primary_lan_netmask
			ip rule del from $primary_lan_network/$primary_lan_netmask
			ip rule add to $primary_lan_network/$primary_lan_netmask table $land_wan_if pref 1000
			ip rule add from $primary_lan_network/$primary_lan_netmask table $land_wan_if pref 1000
			ip route add $primary_lan_network/$primary_lan_netmask proto kernel scope link table $land_wan_if via 0.0.0.0 dev br-$route_lan_section src $ipaddr
			return
		fi

		ret=""
		add_able_default_route $primary_apn_if
		ret=$?

		if [ $ret"" == "0" ];then
			ip rule del to $primary_lan_network/$primary_lan_netmask
			ip rule del from $primary_lan_network/$primary_lan_netmask
			ip rule add to $primary_lan_network/$primary_lan_netmask table $primary_apn_if pref 1000
			ip rule add from $primary_lan_network/$primary_lan_netmask table $primary_apn_if pref 1000
			ip route add $primary_lan_network/$primary_lan_netmask proto kernel scope link table $primary_apn_if via 0.0.0.0 dev br-$route_lan_section src $ipaddr
			return
		fi

	fi

	## when the land does not connect , let the primary apn set to main route
	local land_inter=""
	local primary_inter=""
	local lan_section=""
	for temp in $wan_sections_v6
	do
		config_get xland_wan $temp land_wan
		if [ $xland_wan"" == "1" ];then
			config_get land_inter $temp ifname
		fi
	done

	[ $land_inter"" != "" ] && {

		ping6 -w 3  -I $land_inter 2400:da00::6666 > /dev/null
		if [ $? != '0' ];then
			for temp in $wan_sections_v6
			do
				config_get xprimary_apn $temp primary_apn
				if [ $xprimary_apn"" == "1" ];then
					config_get primary_inter $temp ifname
					config_get lan_section $temp route_lan_section
					local next_holt=`ip -6 route list table ${primary_inter}_v6 | grep default | awk '{print $2}'`
					ip -6 route add default via $next_holt dev ${primary_inter}  proto static  metric 512

					#delete the ipv6 address 
					for temp in `ip addr show dev  br-${lan_section} | grep inet6 | grep global  |  awk '{print $2}'`
					do
						ip addr del $temp dev br-${lan_section}
					done
					local prefix=`ip -6 route list table ${primary_inter}_v6 | grep br-${lan_section}| awk '{print $1}'`
					ip addr add ${prefix} dev br-${lan_section}
				fi
			done
			
			
		fi

	} 

}

#2019.6.25 fengyz
lock_ecgi()
{
	
	local ecgi_enable=`uci get tozed.modem.TZ_ENABLE_ECGI_LOCK`
	if [ $ecgi_enable"" == "1" ];then

		local ecgi=` cat /tmp/.system_info_dynamic | grep CELL_ID | awk -F ':' '{print $2}'`
		local plmn=`cat /tmp/.system_info_dynamic | grep PLMN | awk -F ':' '{print $2}'`
		local ecgi_lock_list=`uci get tozed.modem.TZ_ECGI_ID_LIST`

		local ecgi_hex=`printf "%X" $ecgi | tr '[a-z]' '[A-Z]'` 
		local plmn_hex=`printf "%X" $plmn | tr '[a-z]' '[A-Z]'`

		if [ $ecgi_hex"" == "" -o $plmn_hex"" == "" ];then
			return
		fi 

		local apn0_ifname=`uci get network.4g.ifname`
		local apn1_ifname=`uci get network.4g1.ifname`
		local apn3_ifname=`uci get network.4g3.ifname`


		for temp in $ecgi_lock_list
		do
			local len=`echo $temp | wc -m`
			local lock_ecgi=`echo ${temp:$(($len -8)):7} | tr '[a-z]' '[A-Z]'`
			local lock_plmn=`echo ${temp:0:$(($len -8))} | tr '[a-z]' '[A-Z]'`
			
			if [ $lock_ecgi"" == "" -o $lock_plmn"" == "" ];then
				continue
			fi 

			if [ $ecgi_hex"" == $lock_ecgi"" -a $plmn_hex"" == $lock_plmn"" ];then
					logger "ecgi_lock:remove forbidden network status"

					[ $apn0_ifname"" != "" ] &&{
						iptables -D OUTPUT -o $apn0_ifname -m comment --comment "ecgi network forbidden" -j DROP
					}
					[ $apn1_ifname"" != "" ] &&{
						iptables -D OUTPUT -o $apn1_ifname -m comment --comment "ecgi network forbidden" -j DROP
					}
					[ $apn3_ifname"" != "" ] &&{
						iptables -D OUTPUT -o $apn3_ifname -m comment --comment "ecgi network forbidden" -j DROP
					}
					
					rm -f /tmp/.ecgi_network_forbidden
					return 
			fi

		done

		logger "ecgi_lock:enter forbidden network status"
		local count=`iptables -S | grep "ecgi network forbidden" | wc -l`
		if [ $count"" == "0" ];then
			[ $apn0_ifname"" != "" ] &&{
				iptables -I OUTPUT -o $apn0_ifname -m comment --comment "ecgi network forbidden" -j DROP
			}
			[ $apn1_ifname"" != "" ] &&{
				iptables -I OUTPUT -o $apn1_ifname -m comment --comment "ecgi network forbidden" -j DROP
			}
			[ $apn3_ifname"" != "" ] &&{
				iptables -I OUTPUT -o $apn3_ifname -m comment --comment "ecgi network forbidden" -j DROP
			}
		fi
		touch /tmp/.ecgi_network_forbidden

	fi

}

check_netlink()
{
	while [ 1 ] 
	do
		logger "check_4g_network"
		check_4g_network	
		send_at "at\$qcrmcall?"
		local ret1=`cat ${REVAT_FILE} | grep "2" | wc -l`	
		local ret2=`cat ${REVAT_FILE} | grep "3" | wc -l`	
		if [ $ret1"" -eq '0' ];then
			dial_mutilapn1
		fi

		if [ $ret2"" -eq '0' ];then
			dial_mutilapn2
		fi

		logger "refresh_info 1"
		refresh_info 1
		logger "refresh_info 2"
		refresh_info 2
		logger "refresh_info 3"
		refresh_info 3
		logger "refresh_eth"
		refresh_eth
		logger "check_route"
		check_route
		logger "lock_ecgi"
		lock_ecgi
		logger "Sleep ..."
		sleep 30
	done
}

eval `cfg -e --section modem`

check_4g_network	

dial_mutilapn1

dial_mutilapn2

load_interface

check_netlink
